//
//  ZYNetworking.m
//  ZYNetworking
//
//  Created by 张一奇 on 16/12/5.
//  Copyright © 2016年 zhangYiQi. All rights reserved.
//
#define IsNilString(__String)   (__String==nil || [__String isEqualToString:@"null"] || [__String isEqualToString:@"<null>"])

#import "ZYNetworking.h"
#import "ZYAFNModel.h"

@interface ZYNetworking ()

@end

@implementation ZYNetworking

#pragma mark - 开始监听网络
+ (void)networkStatusWithBlock:(ZYFNetworkStatus)networkStatus
{
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{

        AFNetworkReachabilityManager *reachabilityManager = [AFNetworkReachabilityManager sharedManager];
        [reachabilityManager setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status) {
            switch (status)
            {
                case AFNetworkReachabilityStatusUnknown:
                    networkStatus ? networkStatus(ZYNetworkStatusUnknown) : nil;
                    NSLog(@"未知网络");
                    break;
                case AFNetworkReachabilityStatusNotReachable:
                    networkStatus ? networkStatus(ZYNetworkStatusNotReachable) : nil;
                    NSLog(@"无网络");
//                    networkReachabilityStatusUnknown();
                    break;
                case AFNetworkReachabilityStatusReachableViaWWAN:
                    networkStatus ? networkStatus(ZYNetworkStatusReachableViaWWAN) : nil;
                    NSLog(@"手机自带网络");
//                    networkReachabilityStatusReachableViaWWAN();
                    break;
                case AFNetworkReachabilityStatusReachableViaWiFi:
                    networkStatus ? networkStatus(ZYNetworkStatusReachableViaWiFi) : nil;
                    NSLog(@"WIFI");
                    break;
            }
        }];

        [reachabilityManager startMonitoring];
    });
}
+ (BOOL)isNetwork
{
    return [AFNetworkReachabilityManager sharedManager].reachable;
}

+ (BOOL)isWWANNetwork
{
    return [AFNetworkReachabilityManager sharedManager].reachableViaWWAN;
}

+ (BOOL)isWiFiNetwork
{
    return [AFNetworkReachabilityManager sharedManager].reachableViaWiFi;
}


+ (instancetype)sharedNetworkTool
{
    static ZYNetworking *instance;
    
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        
//        // 设置相对路径
//        NSURL *BaseURL = [NSURL URLWithString:@"https://testapi.moviewisdom.cn/api/v2/?"];
//        // 实例化Manager
//        instance = [[self alloc] initWithBaseURL:BaseURL];

        instance = [self new];
        // 设置请求和相应的 解析器
        instance.requestSerializer = [AFJSONRequestSerializer serializer];
        // 设置超时时间
        [instance.requestSerializer willChangeValueForKey:@"timeoutInterval"];
        instance.requestSerializer.timeoutInterval = 8.f;
        [instance.requestSerializer didChangeValueForKey:@"timeoutInterval"];
        // 增加AFN支持的文件类型
        instance.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json", @"text/json", @"text/javascript", @"text/html", @"text/plain", nil];
        
    });
    
    return instance;
}




- (void)request:(ZYHttpMethod)method componentUrlString:(NSString *)urlString parameters:(id)parameters completion:(void(^)(id response, NSError *error))completion {
    
    // 请求成功的block
    void(^successBlock)(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) = ^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        
//        ZYAFNModel *AFNModel = [ZYAFNModel yy_modelWithJSON:responseObject];
        // 请求成功
        completion(responseObject, nil);
    };
    
    // 请求失败的block
    void(^failureBlock)(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) = ^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        // 请求失败
        completion(nil, error);
        NSLog(@"请求错误%@", error);
    };
    
    if (method == ZYHttpMethodGet) {
        [self GET:urlString parameters:parameters progress:nil success:successBlock failure:failureBlock];
    }else{
        // 发送post请求
        [self POST:urlString parameters:parameters progress:nil success:successBlock failure:failureBlock];
    }
    
}


// 取消所有正在下载线程
- (void)cancelOperations{
    // controller pop回去的时候取消所有正在下载的线程
    [self.operationQueue cancelAllOperations];
}


@end
